package org.dromara.run.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.run.domain.bo.ApplicationBo;
import org.dromara.run.domain.vo.ApplicationVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.run.domain.vo.LevelVo;
import org.dromara.run.domain.bo.LevelBo;
import org.dromara.run.service.ILevelService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 阳光跑等级
 *
 * @author chl
 * @date 2024-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/run/level")
public class LevelController extends BaseController {

    private final ILevelService levelService;

    /**
     * 查询阳光跑等级列表
     */
    @SaCheckPermission("run:level:list")
    @GetMapping("/list")
    public TableDataInfo<LevelVo> list(LevelBo bo, PageQuery pageQuery) {
        return levelService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询阳光跑等级列表详细信息
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("run:level:query")
    @GetMapping("/infoList")
    public R<List<LevelVo>> getInfoList(LevelBo bo) {
        return R.ok(levelService.queryList(bo));
    }

    /**
     * 导出阳光跑等级列表
     */
    @SaCheckPermission("run:level:export")
    @Log(title = "阳光跑等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LevelBo bo, HttpServletResponse response) {
        List<LevelVo> list = levelService.queryList(bo);
        ExcelUtil.exportExcel(list, "阳光跑等级", LevelVo.class, response);
    }

    /**
     * 获取阳光跑等级详细信息
     *
     * @param levelId 主键
     */
    @SaCheckPermission("run:level:query")
    @GetMapping("/{levelId}")
    public R<LevelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long levelId) {
        return R.ok(levelService.queryById(levelId));
    }

    /**
     * 新增阳光跑等级
     */
    @SaCheckPermission("run:level:add")
    @Log(title = "阳光跑等级", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LevelBo bo) {
        return toAjax(levelService.insertByBo(bo));
    }

    /**
     * 修改阳光跑等级
     */
    @SaCheckPermission("run:level:edit")
    @Log(title = "阳光跑等级", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LevelBo bo) {
        return toAjax(levelService.updateByBo(bo));
    }

    /**
     * 删除阳光跑等级
     *
     * @param levelIds 主键串
     */
    @SaCheckPermission("run:level:remove")
    @Log(title = "阳光跑等级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{levelIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] levelIds) {
        return toAjax(levelService.deleteWithValidByIds(List.of(levelIds), true));
    }
}
